<script setup>
import { postTableData } from "@/api/table";
import FirstTitle from "@comp/FirstTitle";
import { onMounted } from "vue";
import { searchDataPzzh } from "@/api/search";
import { addOrUpdatePzzh } from "@/api/addOrUpdate";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

const city = window.globalObj.name;

const editorRef10 = shallowRef();
const valueHtml10 = ref(
  "共XX个掩蔽场所,总面积XX㎡，可掩蔽XX人。具体情况如下："
);
const toolbarConfig10 = {};
const editorConfig10 = { placeholder: "请输入内容..." };
const mode10 = ref("default");
const handleCreated10 = (editor) => {
  editorRef10.value = editor; // 记录 editor 实例，重要！
};
const editorRef11 = shallowRef();
const valueHtml11 = ref(
  `平战转换涉及的掩蔽场所XX个（具体情况见附件5）；按战时人均防护面积XX㎡以及总需求量和现有数量对比计算需增加掩蔽场所XX㎡，可掩蔽XX人。具体情况如下：`
);
const toolbarConfig11 = {};
const editorConfig11 = { placeholder: "请输入内容..." };
const mode11 = ref("default");
const handleCreated11 = (editor) => {
  editorRef11.value = editor; // 记录 editor 实例，重要！
};

// 表头单元格样式
const headerCellStyle = () => {
  return {
    backgroundColor: "#f5f7fa",
    color: "rgba(0,0,0,0.85)",
    fontSize: "14px",
    height: "54px",
  };
};
// 单元格样式
const cellStyle = () => {
  return {
    color: "rgba(0,0,0,0.65)",
    fontSize: "14px",
    lineHeight: "22px",
  };
};

const tableData = ref([]);

// 掩蔽场所建设情况统计表
const jsqkList = ref([]);
const getJsqkData = () => {
  return postTableData({
    docType: "spaceToWar",
    method: "zhebichangsuojiansheList",
  }).then((res) => {
    if (res.code === 200) {
      jsqkList.value = res?.data || [];
    }
  });
};
// 掩蔽场所建设需求清单统计表
const xqqdList = ref([]);
const getXqqdData = () => {
  return postTableData({
    docType: "spaceToWar",
    method: "yanbichangsuoxuqiuList",
  }).then((res) => {
    if (res.code === 200) {
      xqqdList.value = res?.data || [];
    }
  });
};
// 加载html
const initHtml = () => {
  searchDataPzzh({ m: "任务需求掩蔽场所能力现状及需求分析" }).then((res) => {
    const { data } = res;
    valueHtml10.value = data.find((x) => x.k === "掩蔽场所能力现状")?.v || "";
    valueHtml11.value = data.find((x) => x.k === "掩蔽场所需求分析")?.v || "";

  });
};
const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdatePzzh({
      m: "任务需求掩蔽场所能力现状及需求分析",
      data: [
        {
          k: "掩蔽场所能力现状",
          v: valueHtml10.value,
        },
        {
          k: "掩蔽场所需求分析",
          v: valueHtml11.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(async () => {
  initHtml();
  await getJsqkData();
  await getXqqdData();
});

onBeforeUnmount(() => {
  if (editorRef10.value) editorRef10.value.destroy();
  if (editorRef11.value) editorRef11.value.destroy();
});
</script>
<template>
  <div>
    <FirstTitle name="能力现状" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef10"
        :defaultConfig="toolbarConfig10"
        :mode="mode10"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml10"
        :defaultConfig="editorConfig10"
        :mode="mode10"
        @onCreated="handleCreated10"
      />
    </div>
    <div class="top-15 text-center">{{ city }}掩蔽场所建设情况统计表</div>
    <el-table
      id="table"
      :data="jsqkList"
      class="top-15"
      style="width: 100%"
      :header-cell-style="headerCellStyle"
      :cell-style="cellStyle"
      border
    >
      <el-table-column prop="rowNum" label="序号" width="60" align="center" />
      <el-table-column prop="jd_name" label="行政区域" />
      <el-table-column prop="num" label="数量（个）" />
      <el-table-column prop="area" label="面积（㎡）" />
      <el-table-column prop="accept_population" label="可掩蔽人数(人)" />
    </el-table>

    <FirstTitle name="需求分析" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef11"
        :defaultConfig="toolbarConfig11"
        :mode="mode11"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml11"
        :defaultConfig="editorConfig11"
        :mode="mode11"
        @onCreated="handleCreated11"
      />
    </div>
    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>